<?php
// +----------------------------------------------------------------------
// | Description: 1
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-02-05 14:23:24
// +----------------------------------------------------------------------

namespace app\client\controller;

use app\common\controller\Rest;
use think\Db;

class OrderRefunds extends Rest
{
    public $modelName = 'OrderRefund';

    public function save()
    {
        $orderInfo = Db::table('shop_orders')->where(['Id' => $this->param['OrderId']])->find();
        $this->abort($orderInfo, '订单不存在！');
        $this->param['PreOrderStatus'] = $orderInfo['OrderStatus'];
        $res = self::$model->createData($this->param);
        // 修改订单状态 
        Db::table('shop_orders')->where(['Id' => $this->param['OrderId']])->update(['OrderStatus' => 6]);
        $this->checkRes($res, '添加成功', self::$model->getError());
    }

    /**
     * 退款申请审核
     *
     * @return void
     */
    public function update()
    {
        $refundInfo = Db::table('shop_orders_refund')->where(['Id' => $this->param['Id']])->find();
        // 退款生效
        if($this->param['Status'] == 2){
            $res = self::$model->updateDataById($this->param, $this->param['id']);
            Db::table('shop_orders')->where(['Id' => $refundInfo['OrderId']])->update(['OrderStatus' => 7]);
            $this->checkRes(true, '添加成功1');
        }
        // 退款驳回
        if($this->param['Status'] == 3){
            $res = self::$model->updateDataById($this->param, $this->param['id']);
            Db::table('shop_orders')->where(['Id' => $refundInfo['OrderId']])->update(['OrderStatus' => $refundInfo['PreOrderStatus']]);
            $this->checkRes(true, '添加成功2');
        }

        $res = self::$model->updateDataById($this->param, $this->param['id']);
        $this->checkRes($res, '编辑成功3', self::$model->getError());
        
    }
}
